<template>
  <div class="login-account">
    <el-form label-width="64px" :rules="rules" :model="account" size="large" ref="formRef">
      <el-form-item label="账号" prop="username">
        <el-input type="text" v-model="account.username" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="account.password" show-password />
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, ref } from 'vue'
import { rules } from '../../config/accountConfig'
import { ElForm } from 'element-plus'
import { useStore } from 'vuex'
import LocalCache from '@/utils/LocalCache'

export default defineComponent({
  name: 'LoginAccount',
  setup() {
    const store = useStore()
    const account = reactive({
      username: LocalCache.getCache('username') ?? '',
      password: LocalCache.getCache('password') ?? ''
    })
    const formRef = ref<InstanceType<typeof ElForm>>()
    const login = (isSavePassword: boolean) => {
      formRef.value?.validate((valid) => {
        if (valid) {
          store.dispatch('login/accountLoginAction', { ...account, isSavePassword })
        }
      })
    }
    return {
      account,
      formRef,
      login,
      rules
    }
  }
})
</script>

<style scoped lang="less"></style>
